﻿using CQRSFramework.Commands;
using CQRSFramework.Common.Components;
using CQRSFramework.Common.Configurations;
using CQRSFramework.Events;
using CQRSFramework.Infrustructures;
using CQRSFramework.Messages;
using CQRSFramework.RabbitMQ.Impl;
using RabbitMQ.Client;

namespace CQRSFramework.RabbitMQ
{
    public static class ConfigurationExtensions
    {
        public static CommonConfiguration UseRabbitMQ(this CommonConfiguration configuration, string hostName, string userName, string password, int port, string virtualHost, string exchange)
        {
            ObjectContainer.Register<ICommandBus, CommandBus>();
            ObjectContainer.Register<IMessagePublisher, RabbitMQMessagePublisher>();
            ObjectContainer.RegisterInstance<IRabbitMQPersistentConnection, RabbitMQPersistentConnection>(new RabbitMQPersistentConnection(new ConnectionFactory() { HostName = hostName, Port = port, UserName = userName, Password = password, VirtualHost = virtualHost }) { });
            ObjectContainer.Register<IDomainEventPublisher, DomainEventPublisher>();
            return configuration;
        }
        public static CommonConfiguration StartRabbitMQSubscriber(this CommonConfiguration configuration, string commandTopic, string eventTopic)
        {
            new CommandConsumer().Subscribe(commandTopic);
            new EventConsumer().Subscribe(eventTopic);
            return configuration;
        }
    }
}
